package cascade.components
{
	import mx.controls.TextInput;
	import flash.events.FocusEvent;
	import mx.events.FlexEvent;
	import flash.utils.setTimeout;

	public class HelpInput extends TextInput
	{
		protected var helpString:String;
		
		public function HelpInput()  {
			setStyle("font-style", "italic");
			setStyle("color", 0x999999);
			addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
			addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
		}
		
		protected function showHelpString():void {
			setStyle("fontStyle", "italic");
			setStyle("color", 0x999999);
			text = helpString;
		}
		
		protected function hideHelpString():void {
			setStyle("fontStyle", null);
			setStyle("color", NaN);
			text = data as String;
		}
		
		protected function onCreationComplete(event:FlexEvent):void {
			if (text) {
				helpString = text;
				data = "";
			} else {
				hideHelpString()
			}
		}
		
		protected function onFocusIn(event:FocusEvent):void {
			if (!data)
				hideHelpString();
			callLater(delaySelectAll);
		}
		
		protected function delaySelectAll():void {
			setFocus();
			setSelection(0, text.length);
		}
		
		protected function onFocusOut(event:FocusEvent):void {
			data = text;
			if (data == "")
				showHelpString();
			else
				hideHelpString();
		}
		
		public override function set data(value:Object):void {
			super.data = value;
			if (value == "")
				showHelpString();
			else
				hideHelpString();
		}
	}
}